package top.wuyang.study.java24.servlet.footballclub;

import top.wuyang.study.java24.dao.TbFootballClubDAO;
import top.wuyang.study.java24.entity.Result;
import top.wuyang.study.java24.entity.TbFootballClub;
import top.wuyang.study.java24.util.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "footballclub", urlPatterns = "/footballclub/update.action")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        resp.setContentType("application/json");
        try {
            Integer id = Integer.parseInt(req.getParameter("id"));
            String tname = req.getParameter("tname");
            String league = req.getParameter("league");
            String intro = req.getParameter("intro");
            Integer champions = Integer.parseInt(req.getParameter("champions"));
            Integer cups = Integer.parseInt(req.getParameter("cups"));
            Integer fans = Integer.parseInt(req.getParameter("fans"));
            Integer goal = Integer.parseInt(req.getParameter("goal"));
            String gname = req.getParameter("gname");

            TbFootballClub football = new TbFootballClub();
            football.setId(id);
            football.setTname(tname);
            football.setLeague(league);
            football.setIntro(intro);
            football.setChampions(champions);
            football.setCups(cups);
            football.setFans(fans);
            football.setGoal(goal);
            football.setGname(gname);

            int i = TbFootballClubDAO.update(football);
            if (i==1){
                Result<String> result = Result.success("修改成功", null);
                writer.println(JsonUtils.stringify(result));
            }
            else {
                Result<String> result = Result.fail("修改失败", null);
                writer.println(JsonUtils.stringify(result));
            }

        }catch (Exception e) {
            e.printStackTrace();
            Result<String> result = Result.fail("修改发生错误", null);
            writer.println(JsonUtils.stringify(result));
        }




    }
}
